﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAONguyenVatLieu:DataProvider
    {
        public static DataTable getAll()
        {
            string select = "select * from nguyenvatlieu";
            var da = new SqlDataAdapter(select, KetNoi());
            DataTable dt = new DataTable();
            da.Fill(dt);
            DongKetNoi();
            return dt;
        }

        public static int add(DTONguyenVatLieu obj)
        {
            string select = "select * from nguyenvatlieu";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            var row = dt.NewRow();
            row["MaLoai"] = obj.MaLoai;
            row["TenNVL"] = obj.TenNVL;
            row["MaNVL"] = obj.MaNVL;
            dt.Rows.Add(row);
            DongKetNoi();
            return da.Update(dt);
        }

        public static int edit(DTONguyenVatLieu obj, string old_ma)
        {
            string select = "select * from nguyenvatlieu";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(old_ma);
            row.BeginEdit();
            row["MaLoai"] = obj.MaLoai;
            row["TenNVL"] = obj.TenNVL;
            row["MaNVL"] = obj.MaNVL;
            row.EndEdit();

            DongKetNoi();
            return da.Update(dt);
        }

        public static int del(DTONguyenVatLieu obj)
        {
            string select = "select * from nguyenvatlieu";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            var row = dt.Rows.Find(obj.MaNVL);
            row.Delete();

            DongKetNoi();
            return da.Update(dt);
        }

        public static bool checkDuplicate(DTONguyenVatLieu obj, string old_ma)
        {
            var da = new SqlDataAdapter();
            if (old_ma == null)
            {
                string select = "select * from nguyenvatlieu where manvl=@manvl";
                da = new SqlDataAdapter(select, KetNoi());
                da.SelectCommand.Parameters.AddWithValue("@manvl", obj.MaNVL);                
            }
            else
            {
                string select = "select * from nguyenvatlieu where manvl=@manvl and manvl!=@old_ma";
                da = new SqlDataAdapter(select, KetNoi());
                da.SelectCommand.Parameters.AddWithValue("@manvl", obj.MaNVL);
                da.SelectCommand.Parameters.AddWithValue("@old_ma", old_ma);
            }
            
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.Fill(dt);

            bool ret = dt.Rows.Count > 0 ? true : false;
            DongKetNoi();
            return ret;
        }
    }
}
